In [ ]:
# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session
In [13]:
import torch, gc
gc.collect()
torch.cuda.empty_cache()
In [1]:
import torch

if torch.cuda.is_available():
   
    num_devices = torch.cuda.device_count()
    print(f"Number of available GPUs: {num_devices}")
    
   
    for i in range(num_devices):
        device_name = torch.cuda.get_device_name(i)
        print(f"GPU {i}: {device_name}")
        
    
    max_memory_allocated = torch.cuda.max_memory_allocated()
    max_memory_reserved = torch.cuda.max_memory_reserved()
    print(f"Maximum GPU memory allocated: {max_memory_allocated/1024**3:.2f} GB")
    print(f"Maximum GPU memory reserved: {max_memory_reserved/1024**3:.2f} GB")
    print("CUDA is available:", torch.cuda.is_available())
else:
    print("CUDA is not available")
Number of available GPUs: 2
GPU 0: Tesla T4
GPU 1: Tesla T4
Maximum GPU memory allocated: 0.00 GB
Maximum GPU memory reserved: 0.00 GB
CUDA is available: True
In [2]:
import torch
from torch.utils.data import DataLoader, TensorDataset
from sklearn.model_selection import train_test_split

if torch.cuda.is_available():
   
    num_devices = torch.cuda.device_count()
    print(f"Number of available GPUs: {num_devices}")
else:
    print("CUDA is not available")
/opt/conda/lib/python3.10/site-packages/scipy/__init__.py:146: UserWarning: A NumPy version >=1.16.5 and <1.23.0 is required for this version of SciPy (detected version 1.23.5
  warnings.warn(f"A NumPy version >={np_minversion} and <{np_maxversion}"
Number of available GPUs: 2
In [3]:
import numpy as np
from numpy import expand_dims
import pandas as pd
import json
import matplotlib.pyplot as plt
import seaborn as sns
import os
import sys
In [4]:
with open('/kaggle/input/ships-in-satellite-imagery/shipsnet.json') as data_file:
    dataset = json.load(data_file)
shipsnet= pd.DataFrame(dataset)
shipsnet.head()
Out[4]:
data labels locations scene_ids
0 [82, 89, 91, 87, 89, 87, 86, 86, 86, 86, 84, 8... 1 [-118.2254694333423, 33.73803725920789] 20180708_180909_0f47
1 [76, 75, 67, 62, 68, 72, 73, 73, 68, 69, 69, 6... 1 [-122.33222866289329, 37.7491755586813] 20170705_180816_103e
2 [125, 127, 129, 130, 126, 125, 129, 133, 132, ... 1 [-118.14283073363218, 33.736016066914175] 20180712_211331_0f06
3 [102, 99, 113, 106, 96, 102, 105, 105, 103, 10... 1 [-122.34784341495181, 37.76648707436548] 20170609_180756_103a
4 [78, 76, 74, 78, 79, 79, 79, 82, 86, 85, 83, 8... 1 [-122.34852408322172, 37.75878462398653] 20170515_180653_1007
In [5]:
shipsnet.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 4000 entries, 0 to 3999
Data columns (total 4 columns):
 #   Column     Non-Null Count  Dtype 
---  ------     --------------  ----- 
 0   data       4000 non-null   object
 1   labels     4000 non-null   int64 
 2   locations  4000 non-null   object
 3   scene_ids  4000 non-null   object
dtypes: int64(1), object(3)
memory usage: 125.1+ KB
In [6]:
shipsnet = shipsnet[["data", "labels"]]
shipsnet.head()
Out[6]:
data labels
0 [82, 89, 91, 87, 89, 87, 86, 86, 86, 86, 84, 8... 1
1 [76, 75, 67, 62, 68, 72, 73, 73, 68, 69, 69, 6... 1
2 [125, 127, 129, 130, 126, 125, 129, 133, 132, ... 1
3 [102, 99, 113, 106, 96, 102, 105, 105, 103, 10... 1
4 [78, 76, 74, 78, 79, 79, 79, 82, 86, 85, 83, 8... 1
In [7]:
len(shipsnet["data"].iloc[0])
Out[7]:
19200
In [8]:
ship_images = shipsnet["labels"].value_counts()[0]
no_ship_images = shipsnet["labels"].value_counts()[1]
print("Number of the ship_images :{}".format(ship_images),"\n")
print("Number of the no ship_images :{}".format(no_ship_images))
Number of the ship_images :3000 

Number of the no ship_images :1000
In [9]:
x = np.array(dataset['data']).astype('uint8')
y = np.array(dataset['labels']).astype('uint8')
In [10]:
x_reshaped = x.reshape(-1, 3, 80, 80).transpose(0, 2, 3, 1)
x_reshaped = x_reshaped / 255
In [11]:
y_reshaped = np.eye(2)[y]
In [12]:
from sklearn.model_selection import train_test_split

x_train_1, x_test, y_train_1, y_test = train_test_split(x_reshaped, y_reshaped, test_size=0.20, random_state=42)

x_train, x_val, y_train, y_val = train_test_split(x_train_1, y_train_1, test_size=0.25, random_state=42)
In [13]:
from torch.utils.data import Dataset, TensorDataset, DataLoader

x_train_torch = torch.tensor(np.transpose(x_train, (0, 3, 1, 2)), dtype=torch.float32)
y_train_torch = torch.tensor(y_train, dtype=torch.float32)
x_val_torch = torch.tensor(np.transpose(x_val, (0, 3, 1, 2)), dtype=torch.float32)
y_val_torch = torch.tensor(y_val, dtype=torch.float32)

train_dataset = TensorDataset(x_train_torch, y_train_torch)
val_dataset = TensorDataset(x_val_torch, y_val_torch)

train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)
val_loader = DataLoader(val_dataset, batch_size=32, shuffle=False)
In [14]:
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader, TensorDataset
from torchvision.transforms import Normalize
import torch.nn.functional as F


class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 64, 3, padding=1)
        self.bn1 = nn.BatchNorm2d(64)
        self.pool1 = nn.MaxPool2d(2, 2)
        self.dropout1 = nn.Dropout(0.25)

        self.conv2 = nn.Conv2d(64, 64, 3, padding=1)
        self.bn2 = nn.BatchNorm2d(64)
        self.pool2 = nn.MaxPool2d(2, 2)
        self.dropout2 = nn.Dropout(0.25)

        self.conv3 = nn.Conv2d(64, 64, 3, padding=1)
        self.bn3 = nn.BatchNorm2d(64)
        self.pool3 = nn.MaxPool2d(2, 2)
        self.dropout3 = nn.Dropout(0.25)

        self.conv4 = nn.Conv2d(64, 64, 3, padding=1)
        self.bn4 = nn.BatchNorm2d(64)
        self.pool4 = nn.MaxPool2d(2, 2)
        self.dropout4 = nn.Dropout(0.25)

        self.flatten = nn.Flatten()

        self.fc1 = nn.Linear(64 * 5 * 5, 200)
        self.fc2 = nn.Linear(200, 150)
        self.fc3 = nn.Linear(150, 2)

    def forward(self, x):
        x = F.relu(self.bn1(self.conv1(x)))
        x = self.pool1(x)
        x = self.dropout1(x)

        x = F.relu(self.bn2(self.conv2(x)))
        x = self.pool2(x)
        x = self.dropout2(x)

        x = F.relu(self.bn3(self.conv3(x)))
        x = self.pool3(x)
        x = self.dropout3(x)

        x = F.relu(self.bn4(self.conv4(x)))
        x = self.pool4(x)
        x = self.dropout4(x)

        x = self.flatten(x)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = F.softmax(self.fc3(x), dim=1)
        return x

model = Net()
# Move the model to GPU(s)
if torch.cuda.is_available():
    model = model.cuda()
    if torch.cuda.device_count() > 1:
        model = nn.DataParallel(model)
        
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters())

num_epochs = 100
early_stop_patience = 10
best_val_loss = float('inf')
counter = 0

train_losses = []
train_accuracies = []
val_losses = []
val_accuracies = []

for epoch in range(num_epochs):
    train_loss = 0
    train_correct = 0
    val_loss = 0
    val_correct = 0

    model.train()
    running_loss = 0.0
    for i, (inputs, labels) in enumerate(train_loader):
        inputs = inputs.cuda()
        labels = labels.cuda().argmax(dim=1)  # Convert one-hot encoded labels to class indices
        optimizer.zero_grad()
        outputs = model(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()
        running_loss += loss.item()

        train_loss += loss.item()
        _, predicted = torch.max(outputs, 1)
        train_correct += (predicted == labels).sum().item()

    model.eval()
    val_loss = 0.0
    with torch.no_grad():
        for inputs, labels in val_loader:
            inputs = inputs.cuda()
            labels = labels.cuda().argmax(dim=1)  # Convert one-hot encoded labels to class indices
            outputs = model(inputs)
            loss = criterion(outputs, labels)

            val_loss += loss.item()
            _, predicted = torch.max(outputs, 1)
            val_correct += (predicted == labels).sum().item()

    train_losses.append(train_loss / len(train_loader))
    train_accuracies.append(100 * train_correct / len(train_loader.dataset))
    val_losses.append(val_loss / len(val_loader))
    val_accuracies.append(100 * val_correct / len(val_loader.dataset))

    print(f"Epoch {epoch + 1}/{num_epochs}, Train Loss: {train_losses[-1]:.4f}, Train Acc: {train_accuracies[-1]:.2f}%, Val Loss: {val_losses[-1]:.4f}, Val Acc: {val_accuracies[-1]:.2f}%")

    if val_loss < best_val_loss:
        best_val_loss = val_loss
        torch.save(model.state_dict(), 'best_model.pth')
        counter = 0
    else:
        counter += 1
        if counter >= early_stop_patience:
            print(f"Early stopping after {counter} epochs with no improvement.")
            break

model.load_state_dict(torch.load('best_model.pth'))
Epoch 1/100, Train Loss: 0.4542, Train Acc: 85.92%, Val Loss: 0.4644, Val Acc: 84.25%
Epoch 2/100, Train Loss: 0.3912, Train Acc: 92.08%, Val Loss: 0.3747, Val Acc: 93.62%
Epoch 3/100, Train Loss: 0.3613, Train Acc: 95.17%, Val Loss: 0.3712, Val Acc: 95.00%
Epoch 4/100, Train Loss: 0.3485, Train Acc: 96.58%, Val Loss: 0.3704, Val Acc: 93.88%
Epoch 5/100, Train Loss: 0.3511, Train Acc: 96.04%, Val Loss: 0.3437, Val Acc: 97.00%
Epoch 6/100, Train Loss: 0.3348, Train Acc: 97.75%, Val Loss: 0.3456, Val Acc: 96.62%
Epoch 7/100, Train Loss: 0.3314, Train Acc: 98.17%, Val Loss: 0.3409, Val Acc: 97.00%
Epoch 8/100, Train Loss: 0.3339, Train Acc: 97.96%, Val Loss: 0.3507, Val Acc: 96.00%
Epoch 9/100, Train Loss: 0.3336, Train Acc: 97.96%, Val Loss: 0.3480, Val Acc: 96.38%
Epoch 10/100, Train Loss: 0.3291, Train Acc: 98.46%, Val Loss: 0.3458, Val Acc: 96.75%
Epoch 11/100, Train Loss: 0.3261, Train Acc: 98.62%, Val Loss: 0.3342, Val Acc: 98.00%
Epoch 12/100, Train Loss: 0.3225, Train Acc: 99.12%, Val Loss: 0.3353, Val Acc: 97.75%
Epoch 13/100, Train Loss: 0.3253, Train Acc: 98.71%, Val Loss: 0.3295, Val Acc: 98.38%
Epoch 14/100, Train Loss: 0.3264, Train Acc: 98.62%, Val Loss: 0.3415, Val Acc: 97.12%
Epoch 15/100, Train Loss: 0.3227, Train Acc: 99.00%, Val Loss: 0.3290, Val Acc: 98.38%
Epoch 16/100, Train Loss: 0.3195, Train Acc: 99.42%, Val Loss: 0.3303, Val Acc: 98.25%
Epoch 17/100, Train Loss: 0.3186, Train Acc: 99.50%, Val Loss: 0.3329, Val Acc: 98.12%
Epoch 18/100, Train Loss: 0.3190, Train Acc: 99.50%, Val Loss: 0.3368, Val Acc: 97.62%
Epoch 19/100, Train Loss: 0.3246, Train Acc: 98.83%, Val Loss: 0.3395, Val Acc: 97.38%
Epoch 20/100, Train Loss: 0.3231, Train Acc: 98.96%, Val Loss: 0.3307, Val Acc: 98.25%
Epoch 21/100, Train Loss: 0.3213, Train Acc: 99.25%, Val Loss: 0.3349, Val Acc: 97.88%
Epoch 22/100, Train Loss: 0.3188, Train Acc: 99.46%, Val Loss: 0.3310, Val Acc: 98.25%
Epoch 23/100, Train Loss: 0.3174, Train Acc: 99.62%, Val Loss: 0.3279, Val Acc: 98.50%
Epoch 24/100, Train Loss: 0.3277, Train Acc: 98.54%, Val Loss: 0.3470, Val Acc: 96.62%
Epoch 25/100, Train Loss: 0.3252, Train Acc: 98.67%, Val Loss: 0.3408, Val Acc: 97.12%
Epoch 26/100, Train Loss: 0.3226, Train Acc: 99.04%, Val Loss: 0.3400, Val Acc: 97.12%
Epoch 27/100, Train Loss: 0.3376, Train Acc: 97.46%, Val Loss: 0.3385, Val Acc: 97.38%
Epoch 28/100, Train Loss: 0.3275, Train Acc: 98.50%, Val Loss: 0.3463, Val Acc: 96.62%
Epoch 29/100, Train Loss: 0.3243, Train Acc: 98.88%, Val Loss: 0.3339, Val Acc: 97.75%
Epoch 30/100, Train Loss: 0.3176, Train Acc: 99.58%, Val Loss: 0.3274, Val Acc: 98.62%
Epoch 31/100, Train Loss: 0.3160, Train Acc: 99.71%, Val Loss: 0.3311, Val Acc: 98.12%
Epoch 32/100, Train Loss: 0.3177, Train Acc: 99.58%, Val Loss: 0.3329, Val Acc: 98.00%
Epoch 33/100, Train Loss: 0.3274, Train Acc: 98.54%, Val Loss: 0.3330, Val Acc: 98.00%
Epoch 34/100, Train Loss: 0.3165, Train Acc: 99.67%, Val Loss: 0.3299, Val Acc: 98.25%
Epoch 35/100, Train Loss: 0.3172, Train Acc: 99.62%, Val Loss: 0.3285, Val Acc: 98.25%
Epoch 36/100, Train Loss: 0.3160, Train Acc: 99.71%, Val Loss: 0.3342, Val Acc: 97.75%
Epoch 37/100, Train Loss: 0.3172, Train Acc: 99.58%, Val Loss: 0.3330, Val Acc: 98.00%
Epoch 38/100, Train Loss: 0.3167, Train Acc: 99.71%, Val Loss: 0.3241, Val Acc: 98.88%
Epoch 39/100, Train Loss: 0.3164, Train Acc: 99.71%, Val Loss: 0.3247, Val Acc: 98.88%
Epoch 40/100, Train Loss: 0.3153, Train Acc: 99.79%, Val Loss: 0.3258, Val Acc: 98.75%
Epoch 41/100, Train Loss: 0.3174, Train Acc: 99.58%, Val Loss: 0.3266, Val Acc: 98.62%
Epoch 42/100, Train Loss: 0.3153, Train Acc: 99.79%, Val Loss: 0.3357, Val Acc: 97.75%
Epoch 43/100, Train Loss: 0.3188, Train Acc: 99.46%, Val Loss: 0.3305, Val Acc: 98.25%
Epoch 44/100, Train Loss: 0.3249, Train Acc: 98.79%, Val Loss: 0.3295, Val Acc: 98.38%
Epoch 45/100, Train Loss: 0.3200, Train Acc: 99.33%, Val Loss: 0.3314, Val Acc: 98.12%
Epoch 46/100, Train Loss: 0.3184, Train Acc: 99.46%, Val Loss: 0.3321, Val Acc: 98.00%
Epoch 47/100, Train Loss: 0.3246, Train Acc: 98.92%, Val Loss: 0.3386, Val Acc: 97.50%
Epoch 48/100, Train Loss: 0.3191, Train Acc: 99.42%, Val Loss: 0.3352, Val Acc: 97.88%
Early stopping after 10 epochs with no improvement.
Out[14]:
<All keys matched successfully>
In [15]:
print(model)
DataParallel(
  (module): Net(
    (conv1): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (pool1): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (dropout1): Dropout(p=0.25, inplace=False)
    (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (pool2): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (dropout2): Dropout(p=0.25, inplace=False)
    (conv3): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (bn3): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (pool3): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (dropout3): Dropout(p=0.25, inplace=False)
    (conv4): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
    (bn4): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
    (pool4): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
    (dropout4): Dropout(p=0.25, inplace=False)
    (flatten): Flatten(start_dim=1, end_dim=-1)
    (fc1): Linear(in_features=1600, out_features=200, bias=True)
    (fc2): Linear(in_features=200, out_features=150, bias=True)
    (fc3): Linear(in_features=150, out_features=2, bias=True)
  )
)
In [16]:
import matplotlib.pyplot as plt

plt.figure(figsize=(12, 4))
plt.subplot(1, 2, 1)
plt.plot(train_losses, label="Training Loss")
plt.plot(val_losses, label="Validation Loss")
plt.xlabel("Epoch")
plt.ylabel("Loss")
plt.legend()

plt.subplot(1, 2, 2)
plt.plot(train_accuracies, label="Training Accuracy")
plt.plot(val_accuracies, label="Validation Accuracy")
plt.xlabel("Epoch")
plt.ylabel("Accuracy")
plt.legend()

plt.show()
In [17]:
x_test_torch = torch.tensor(np.transpose(x_test, (0, 3, 1, 2)), dtype=torch.float32).cuda()
y_test_torch = torch.tensor(y_test, dtype=torch.float32).cuda()


test_dataset = TensorDataset(x_test_torch, y_test_torch)
test_loader = DataLoader(test_dataset, batch_size=32, shuffle=False)


model.eval()
correct = 0
total = 0

with torch.no_grad():
    inputs = inputs.cuda()  
    labels = labels.cuda() 
    for inputs, labels in test_loader:
        outputs = model(inputs)
        _, predicted = torch.max(outputs, 1)
        _, labels = torch.max(labels, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()

accuracy = 100 * correct / total
print("Test Accuracy: {:.2f}%".format(accuracy))
Test Accuracy: 99.00%
In [18]:
with torch.no_grad():
    predictions = model(x_test_torch)

probabilities = F.softmax(predictions, dim=1).cpu().numpy()
In [19]:
import pandas as pd

single_prediction = pd.Series(probabilities[2], index=["Not A Ship", "Ship"])
print(single_prediction)
Not A Ship    0.268941
Ship          0.731059
dtype: float32
In [20]:
import matplotlib.pyplot as plt


first_test_image = x_test[2]


plt.imshow(first_test_image)
plt.title("First Test Image")
plt.axis("off")
plt.show()
In [21]:
import cv2
import numpy as np
import matplotlib.pyplot as plt

def scanmap(image_path):
    

    satellite_image = cv2.imread(image_path)

    satellite_image = satellite_image.astype(np.float32) / 255.0

    window_size = (80, 80)
    stride = 10

    height, width, channels = satellite_image.shape

    probabilities_map = []

    for y in range(0, height - window_size[1] + 1, stride):
        row_probabilities = []
        for x in range(0, width - window_size[0] + 1, stride):
            cropped_window = satellite_image[y:y + window_size[1], x:x + window_size[0]]
            cropped_window_torch = torch.tensor(cropped_window.transpose(2, 0, 1), dtype=torch.float32).unsqueeze(0).cpu()
        
            with torch.no_grad():
                probabilities = model(cropped_window_torch)
        
            row_probabilities.append(probabilities[0, 1].item())
    
        probabilities_map.append(row_probabilities)

    probabilities_map = np.array(probabilities_map)

    

    plt.imshow(probabilities_map, cmap='hot', interpolation='nearest')
    plt.colorbar()
    plt.show()
In [22]:
scanmap("/kaggle/input/images/close_1png.png")
In [3]:
using_colab = True
In [4]:
if using_colab:
    import torch
    import torchvision
    print("PyTorch version:", torch.__version__)
    print("Torchvision version:", torchvision.__version__)
    print("CUDA is available:", torch.cuda.is_available())
    import sys
    !{sys.executable} -m pip install opencv-python matplotlib
    !{sys.executable} -m pip install 'git+https://github.com/facebookresearch/segment-anything.git'
    
    !mkdir images
    !wget -P images https://raw.githubusercontent.com/facebookresearch/segment-anything/main/notebooks/images/dog.jpg
        
    !wget https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth
PyTorch version: 2.0.0
Torchvision version: 0.15.1
CUDA is available: True
Requirement already satisfied: opencv-python in /opt/conda/lib/python3.10/site-packages (4.5.4.60)
Requirement already satisfied: matplotlib in /opt/conda/lib/python3.10/site-packages (3.6.3)
Requirement already satisfied: numpy>=1.21.2 in /opt/conda/lib/python3.10/site-packages (from opencv-python) (1.23.5)
Requirement already satisfied: kiwisolver>=1.0.1 in /opt/conda/lib/python3.10/site-packages (from matplotlib) (1.4.4)
Requirement already satisfied: packaging>=20.0 in /opt/conda/lib/python3.10/site-packages (from matplotlib) (21.3)
Requirement already satisfied: python-dateutil>=2.7 in /opt/conda/lib/python3.10/site-packages (from matplotlib) (2.8.2)
Requirement already satisfied: contourpy>=1.0.1 in /opt/conda/lib/python3.10/site-packages (from matplotlib) (1.0.7)
Requirement already satisfied: pillow>=6.2.0 in /opt/conda/lib/python3.10/site-packages (from matplotlib) (9.5.0)
Requirement already satisfied: pyparsing>=2.2.1 in /opt/conda/lib/python3.10/site-packages (from matplotlib) (3.0.9)
Requirement already satisfied: fonttools>=4.22.0 in /opt/conda/lib/python3.10/site-packages (from matplotlib) (4.39.3)
Requirement already satisfied: cycler>=0.10 in /opt/conda/lib/python3.10/site-packages (from matplotlib) (0.11.0)
Requirement already satisfied: six>=1.5 in /opt/conda/lib/python3.10/site-packages (from python-dateutil>=2.7->matplotlib) (1.16.0)
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
Collecting git+https://github.com/facebookresearch/segment-anything.git
  Cloning https://github.com/facebookresearch/segment-anything.git to /tmp/pip-req-build-1zz8twph
  Running command git clone --filter=blob:none --quiet https://github.com/facebookresearch/segment-anything.git /tmp/pip-req-build-1zz8twph
  Resolved https://github.com/facebookresearch/segment-anything.git to commit 6fdee8f2727f4506cfbbe553e23b895e27956588
  Preparing metadata (setup.py) ... done
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
--2023-05-08 19:01:06--  https://raw.githubusercontent.com/facebookresearch/segment-anything/main/notebooks/images/dog.jpg
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.108.133, 185.199.109.133, 185.199.110.133, ...
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.108.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 99846 (98K) [image/jpeg]
Saving to: ‘images/dog.jpg’

dog.jpg             100%[===================>]  97.51K  --.-KB/s    in 0.01s   

2023-05-08 19:01:06 (8.27 MB/s) - ‘images/dog.jpg’ saved [99846/99846]

--2023-05-08 19:01:07--  https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth
Resolving dl.fbaipublicfiles.com (dl.fbaipublicfiles.com)... 52.84.162.119, 52.84.162.20, 52.84.162.103, ...
Connecting to dl.fbaipublicfiles.com (dl.fbaipublicfiles.com)|52.84.162.119|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2564550879 (2.4G) [binary/octet-stream]
Saving to: ‘sam_vit_h_4b8939.pth’

sam_vit_h_4b8939.pt 100%[===================>]   2.39G   241MB/s    in 11s     

2023-05-08 19:01:18 (229 MB/s) - ‘sam_vit_h_4b8939.pth’ saved [2564550879/2564550879]

In [5]:
import numpy as np
import torch
import matplotlib.pyplot as plt
import cv2
In [6]:
image = cv2.imread('/kaggle/input/ships-in-satellite-imagery/scenes/scenes/sfbay_1.png')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
In [7]:
plt.figure(figsize=(20,20))
plt.imshow(image)
plt.axis('off')
plt.show()
In [8]:
import sys
sys.path.append("..")
from segment_anything import sam_model_registry, SamAutomaticMaskGenerator, SamPredictor

sam_checkpoint = "sam_vit_h_4b8939.pth"
model_type = "vit_h"

device = "cuda"

sam = sam_model_registry[model_type](checkpoint=sam_checkpoint)
sam.to(device=device)

mask_generator = SamAutomaticMaskGenerator(sam)
In [9]:
masks = mask_generator.generate(image)
In [10]:
print(len(masks))
print(masks[0].keys())
51
dict_keys(['segmentation', 'area', 'bbox', 'predicted_iou', 'point_coords', 'stability_score', 'crop_box'])
In [11]:
def show_anns(anns):
    if len(anns) == 0:
        return
    sorted_anns = sorted(anns, key=(lambda x: x['area']), reverse=True)
    ax = plt.gca()
    ax.set_autoscale_on(False)
    polygons = []
    color = []
    for ann in sorted_anns:
        m = ann['segmentation']
        img = np.ones((m.shape[0], m.shape[1], 3))
        color_mask = np.random.random((1, 3)).tolist()[0]
        for i in range(3):
            img[:,:,i] = color_mask[i]
        ax.imshow(np.dstack((img, m*0.35)))
In [12]:
plt.figure(figsize=(20,20))
plt.imshow(image)
show_anns(masks)
plt.axis('off')
plt.show() 
In [23]:
import os
import json
import shutil
from PIL import Image
import numpy as np
from sklearn.model_selection import train_test_split

def save_image_from_data(data, label, output_path):
    image_data = np.array(data, dtype='uint8').reshape((80, 80, 3))
    img = Image.fromarray(image_data, 'RGB')
    img.save(output_path)
    return output_path



def split_data_into_train_test(input_path, train_path, test_path, test_ratio=0.2):
    with open(input_path, 'r') as f:
        dataset = json.load(f)

    if not os.path.exists(train_path):
        os.makedirs(train_path)
        os.makedirs(os.path.join(train_path, 'ship'))
        os.makedirs(os.path.join(train_path, 'non_ship'))

    if not os.path.exists(test_path):
        os.makedirs(test_path)
        os.makedirs(os.path.join(test_path, 'ship'))
        os.makedirs(os.path.join(test_path, 'non_ship'))

    images_and_labels = [(save_image_from_data(data, label, f"/kaggle/working/temp/{i}.png"), label)
                         for i, (data, label) in enumerate(zip(dataset['data'], dataset['labels']))]

    train_data, test_data = train_test_split(images_and_labels, test_size=test_ratio, random_state=42, stratify=[label for _, label in images_and_labels])

    for i, (image_path, label) in enumerate(train_data):
        class_folder = 'ship' if label == 1 else 'non_ship'
        shutil.move(image_path, os.path.join(train_path, class_folder, f"{i}.png"))

    for i, (image_path, label) in enumerate(test_data):
        class_folder = 'ship' if label == 1 else 'non_ship'
        shutil.move(image_path, os.path.join(test_path, class_folder, f"{i}.png"))

    shutil.rmtree('/kaggle/working/temp')

input_path = "/kaggle/input/ships-in-satellite-imagery/shipsnet.json"
train_path = "/kaggle/working/train/data"
test_path = "/kaggle/working/test/data"

os.makedirs('/kaggle/working/temp', exist_ok=True)
split_data_into_train_test(input_path, train_path, test_path)
In [25]:
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
from torch.optim import Adam
from torchvision.transforms import Compose, ToTensor, Normalize
from torch.utils.data import DataLoader
from torchvision.datasets import ImageFolder


def load_ship_data(train_path, test_path, train_batch_size=64, test_batch_size=64):
    transform = Compose([
        ToTensor(),
        Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
    ])

    train_loader = DataLoader(
        ImageFolder(train_path, transform=transform),
        batch_size=train_batch_size, shuffle=True)

    test_loader = DataLoader(
        ImageFolder(test_path, transform=transform),
        batch_size=test_batch_size, shuffle=False)

    return train_loader, test_loader


class Net(torch.nn.Module):
    def __init__(self, dims):
        super().__init__()
        self.layers = []
        self.loss_history = []
        self.train_accuracy_history = []
        for d in range(len(dims) - 1):
            self.layers += [Layer(dims[d], dims[d + 1]).cuda()]

    def predict(self, x):
        h = x
        for layer in self.layers:
            h = layer(h)
        return h.argmax(1)

    def train(self, x_pos, x_neg, y):
        h_pos, h_neg = x_pos, x_neg
        for i, layer in enumerate(self.layers):
            print('training layer', i, '...')
            h_pos, h_neg, epoch_losses = layer.train(h_pos, h_neg)
            self.loss_history.extend(epoch_losses)
            train_accuracy = (self.predict(x_pos).eq(y[y == 1]).float().mean().item(),
                              self.predict(x_neg).eq(y[y == 0]).float().mean().item())
            self.train_accuracy_history.append(train_accuracy)


class Layer(nn.Linear):
    def __init__(self, in_features, out_features,
                 bias=True, device=None, dtype=None):
        super().__init__(in_features, out_features, bias, device, dtype)
        self.relu = torch.nn.ReLU()
        self.opt = Adam(self.parameters(), lr=0.03)
        self.threshold = 2.0
        self.num_epochs = 1000

    def forward(self, x):
        x_direction = x / (x.norm(2, 1, keepdim=True) + 1e-4)
        x_flattened = x_direction.reshape(x_direction.size(0), -1)
        return self.relu(
            torch.mm(x_flattened, self.weight.T) +
            self.bias.unsqueeze(0))


    def train(self, x_pos, x_neg):
        epoch_losses = []
        for i in range(self.num_epochs):
            g_pos = self.forward(x_pos).pow(2).mean(1)
            g_neg = self.forward(x_neg).pow(2).mean(1)
            loss = torch.log(1 + torch.exp(torch.cat([
                -g_pos + self.threshold,
                g_neg - self.threshold]))).mean()
            self.opt.zero_grad()
            loss.backward()
            self.opt.step()
            epoch_losses.append(loss.item())
        return self.forward(x_pos).detach(), self.forward(x_neg).detach(), epoch_losses



if __name__ == "__main__":
    torch.manual_seed(1234)

    train_path = "/kaggle/working/train/data"
    test_path = "/kaggle/working/test/data"
    train_loader, test_loader = load_ship_data(train_path, test_path)

    net = Net([19200, 500, 500, 2])  
    x, y = next(iter(train_loader))
    x, y = x.cuda(), y.cuda()
    x_pos = x[y == 1]
    x_neg = x[y == 0]

    net.train(x_pos, x_neg,y)

    x_train, y_train = next(iter(train_loader))
    x_train, y_train = x_train.cuda(), y_train.cuda()
    train_accuracy = net.predict(x_train).eq(y_train).float().mean().item()
    print('train accuracy:', train_accuracy)

    x_test, y_test = next(iter(test_loader))
    x_test, y_test = x_test.cuda(), y_test.cuda()
    test_accuracy = net.predict(x_test).eq(y_test).float().mean().item()
    print('test accuracy:', test_accuracy)
training layer 0 ...
training layer 1 ...
training layer 2 ...
train accuracy: 0.734375
test accuracy: 1.0

Article of FF-Algorithm [Github Link](https://github.com/mohammadpz/pytorch_forward_forward

In [ ]: